package com.chinairi.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chinairi.mall.controller.FileController;
import com.chinairi.mall.controller.viewobject.GoodsInfoVO;
import com.chinairi.mall.controller.viewobject.GoodsPictureVO;
import com.chinairi.mall.error.BusinessException;
import com.chinairi.mall.error.EmBusinessError;
import com.chinairi.mall.model.FourNewGoods;
import com.chinairi.mall.model.GoodsPicture;
import com.chinairi.mall.dao.GoodsPictureMapper;
import com.chinairi.mall.service.IGoodsPictureService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 商品图片 服务实现类
 * </p>
 *
 * @author gsl
 * @since 2020-09-05
 */
@Service
public class GoodsPictureServiceImpl extends ServiceImpl<GoodsPictureMapper, GoodsPicture> implements IGoodsPictureService {


    @Autowired
    private GoodsPictureMapper goodsPictureMapper;

    @Autowired
    private FileController fileController;

    /**
     * 上传图片
     *
     * @param file           图片文件
     * @param goodsFileClass 主图1：幅图:2
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public GoodsPicture uploadPic(MultipartFile file, String goodsFileClass, String goodsDbid) throws IOException {
        //插入商品的图片
        if (file == null) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, "商品未上传图片");
        }
        String fileName = file.getOriginalFilename();
        String picUrl = fileController.uploadFile(file, "picture");
        GoodsPicture goodsPicture = new GoodsPicture();
        //实际访问的路径
        goodsPicture.setGoodsFileService(picUrl);
        goodsPicture.setGoodsPicDbid(UUID.randomUUID().toString().replace("-", ""));
        goodsPicture.setGoodsFileName(fileName);
        goodsPicture.setGoodsFileType(file.getContentType());
        goodsPicture.setGoodsFileSize(String.valueOf(file.getSize()));
        goodsPicture.setGoodsFileClass(goodsFileClass);
        if (goodsDbid != null) {
            goodsPicture.setGoodsDbid(goodsDbid);
            goodsPictureMapper.insert(goodsPicture);
        }
        return goodsPicture;
    }

    /**
     * 储存图片信息
     *
     * @param goodsPicture   图片文件
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void uploadPicNew(GoodsPicture goodsPicture) throws IOException {
        //插入商品的图片
        if (goodsPicture == null) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, "商品未上传图片");
        }
        goodsPicture.setGoodsPicDbid(UUID.randomUUID().toString().replace("-", ""));
        if (goodsPicture.getGoodsDbid() != null) {
            goodsPictureMapper.insert(goodsPicture);
        }
    }

    /**
     * 查询图片
     * 按图片名称、图片类别（主副图）、商品编码
     *
     * @param goodsPictureVO
     * @return
     */
    @Override
    public List<GoodsPicture> searchPic(GoodsPictureVO goodsPictureVO) {
        QueryWrapper<GoodsPicture> wrapper = new QueryWrapper<>();
        String picName = goodsPictureVO.getGoodsFileName();
        String picClass = goodsPictureVO.getGoodsFileClass();
        String goodsDbid = goodsPictureVO.getGoodsDbid();
        wrapper.like(StringUtils.isNotEmpty(picName), "GOODS_FILE_NAME", picName)
                .eq(StringUtils.isNotEmpty(picClass), "GOODS_FILE_CLASS", picClass)
                .eq(StringUtils.isNotEmpty(goodsDbid), "GOODS_DBID", goodsDbid);
        return goodsPictureMapper.selectList(wrapper);
    }

    /**
     * 根据企业id获取商品名称
     *
     * @param csDbid
     * @return
     */
    @Override
    public List<GoodsInfoVO> getGoodsName(String csDbid) {
        return goodsPictureMapper.selectGoodsName(csDbid);
    }

    /**
     * 获取四新商品名称
     *
     * @param csDbId
     * @return
     */
    @Override
    public List<FourNewGoods> getFourNewName(String csDbId) {
        return goodsPictureMapper.selectFourNewName(csDbId);
    }
}
